<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>WebSocket_test</title>
</head>
<body>
<script type="text/javascript">
    var socket;
    if (typeof (WebSocket) == "undefined") {
        console.log("浏览器不支持WebSocket")
    } else {
        console.log("支持WebSocket")

        //实现WebSocket对象，指定要连接的服务器地址和端口，建立连接
        socket = new WebSocket("ws://localhost:8085/websocket/1")

        //打开事件
        socket.onopen = function (ev) {
            console.log("Socket 已打开");
            socket.send("这是来自客户端的信息" + location.href + new Date())
        }

        //获得消息事件
        socket.onmessage = function (ev) {
            console.log("msg==>" + ev.data)
        }

        //关闭事件
        socket.onclose = function (ev) {
            console.log("Socket 已关闭")
        }

        //发生了错误事件
        socket.onerror = function (ev) {
            alert("Socket 发生错误")
        }


    }
</script>

</body>
</html>